<template>
  <div>
    <div class="Box">
      <div v-if="isVoteEd">
        <img class="isImg" src="../../assets/Votinged.png" />
      </div>
      <div v-if="isOver">
        <img class="isOverImg" src="../../assets/isOver.png" />
      </div>
      <!-- 问卷标题 -->
      <div class="title">
        {{ votedata.title }}
      </div>
      <img
        v-if="votedata.images"
        class="headimg"
        :src="votedata.images | votingImg"
      />
      <!-- 问卷描述 -->
      <div v-if="votedata.describe" class="Votingtext">
        {{ votedata.describe }}
      </div>
      <!-- 问卷视频链接 -->
      <div v-if="votedata.videos" class="video">
        视频链接:
        <span @click="openVedio(votedata.videos)">{{ votedata.videos }}</span>
      </div>
      <div>（每人最多可投{{ votedata.selectNum }}票）</div>

      <el-checkbox-group
        :disabled="isVoteEd || isOver"
        class="votinglist"
        v-model="select"
        :min="0"
        :max="votedata.selectNum"
      >
        <div class="votingbox" v-for="(item, index) in selectdata" :key="index">
          <div v-if="item.optionfile" class="imgs">
            <img
              @click.stop="handlePictureCardPreview(item.optionfile)"
              class="imgs"
              :src="item.optionfile | votingImg"
            />
          </div>
          <img
            v-if="select.indexOf(index) > -1 && isVoteEd"
            class="votimg"
            style=""
            src="../../assets/Voting2.png"
          />
          <div v-if="item.optionvideo" class="optionvideo">
            <span @click="openVedio(item.optionvideo)"
              >视频链接：{{ item.optionvideo }}</span
            >
          </div>
          <div v-if="isVoteEd || isOver" class="openimg">
            <span v-if="isVoteEd || isOver">{{ item.optionnum }} 票</span>
          </div>
          <el-checkbox class="checkTitle" :label="index">
            {{ item.optiontitle }}</el-checkbox
          >
        </div>
      </el-checkbox-group>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
      <el-dialog
        width="1400px"
        class="elDog"
        title=""
        :visible.sync="dialogTableVisible"
      >
        <iframe
          v-if="dialogTableVisible"
          frameborder="0"
          width="1200"
          height="800"
          :src="VedioHtml"
          allowFullScreen="true"
        ></iframe>
      </el-dialog>

      <div class="bottom">
        <el-button
          :disabled="isVoteEd || isOver"
          class="bottomButton"
          @click="enterS"
          type="primary"
          >提交投票</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { get_vote, put_vote } from "../../api/vote";
import { get_voteDetail, post_voteDetail } from "../../api/voteDetail";

import { Voting_img_prefix } from "../../utils/global";
// import { formatDateTime } from "../../common/Time";
export default {
  data() {
    return {
      select: [],
      selectdata: [],
      votedata: [],
      dialogImageUrl: "",
      dialogVisible: false,
      isVoteEd: false,
      isOver: false,
      dialogTableVisible: false,
      VedioHtml: ""
    };
  },
  filters: {
    votingImg(val) {
      return Voting_img_prefix + val;
    }
  },
  async created() {
    var req_voteDetail = {
      query: {
        voteId: this.$route.query._id,
        votepeople: this.$store.getters.userInfo._id
      }
    };
    var voteDetail = await get_voteDetail(req_voteDetail);
    if (voteDetail.data.length !== 0) {
      this.isVoteEd = true;
      console.log(voteDetail.data[0].optionDetail);
      var select = [];
      voteDetail.data[0].optionDetail.forEach(element => {
        select.push(element.index);
      });
      this.select = select;
    }
    console.log(this.isVoteEd);
    console.log(this.$route.query);
    var votedata = await get_vote({ query: this.$route.query });
    console.log(votedata);
    this.votedata = votedata.data[0];
    this.selectdata = votedata.data[0].option;
    //判断是否过期
    var nowtime = Date.parse(new Date());
    var endtime = Date.parse(votedata.data[0].endtime);
    if (nowtime > endtime) {
      this.isOver = true;
    }
  },
  methods: {
    openVedio(vedio) {
      console.log(vedio);
      this.VedioHtml = vedio;
      this.dialogTableVisible = true;
    },
    async updateVoting() {
      console.log(this.select);
      var optionDetail = [];
      for (let index = 0; index < this.select.length; index++) {
        const element = this.select[index];
        optionDetail.push({
          index: element,
          data: this.selectdata[element].optiontitle
        });
        put_vote({
          query: { _id: this.$route.query._id },
          update: {
            $inc: { ["option." + element + ".optionnum"]: "1" }
          }
        });
      }
      var select = {
        voteId: this.$route.query._id, //投票id
        optionDetail: optionDetail, //投票选项
        creatTime: new Date(),
        votepeople: this.$store.getters.userInfo._id //此投票的投票人员的_id
      };
      console.log(select);
      await post_voteDetail(select);
      await put_vote({
        query: { _id: this.$route.query._id },
        update: {
          $inc: { voteNum: this.select.length }
        }
      });
      setTimeout(this.$router.go(0), 3000);
    },
    enterS() {
      var that = this;
      this.$confirm("是否确定投票完毕? 确认后无法修改", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          that.updateVoting();
          that.$finish_taskdata(7, this.$store.getters.userInfo._id);
          this.$message({
            type: "success",
            message: "提交成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    handlePictureCardPreview(val) {
      this.dialogImageUrl = Voting_img_prefix + val;
      this.dialogVisible = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.Box {
  position: relative;
  width: 1000px;
  margin: 0 auto;
  background-color: #ffffff;
  height: auto;
  text-align: center;
  padding: 50px;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  .title {
    width: 800px;
    margin: 0 auto;
    text-align: center;
    color: rgb(29, 47, 112);
    font-size: 30px;
    word-wrap: break-word;
  }
  .headimg {
    width: 800px;
    height: auto;
    margin: 20px auto;
    border-radius: 10px;
    border: 1px solid #b3b3b3;
  }
  .Votingtext {
    width: 800px;
    margin: 10px auto;
    word-wrap: break-word;
  }
  .video {
    width: 800px;
    margin: 10px auto;
    word-wrap: break-word;
    display: flex;
    font-weight: 550;
    font-size: 18px;
    word-wrap: break-word;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    color: rgb(29, 47, 112);
  }
  .video :hover {
    cursor: pointer;
    text-decoration: underline;
  }
  .votinglist {
    width: 800px;
    margin: 30px auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    .votingbox {
      position: relative;
      margin: 20px;
      padding: 10px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2), 0 0 6px rgba(0, 0, 0, 0.2);

      border-radius: 10px;
      .imgs {
        height: 134px;
        width: 134px;
        border-radius: 10px;
      }
      .votimg {
        position: absolute;
        bottom: 2px;
        right: 2px;
        z-index: 100;
        width: 53px;
        height: 44px;
      }
      .optionvideo {
        height: 40px;
        width: 134px;
        display: flex;
        font-weight: 550;
        font-size: 12px;
        word-wrap: break-word;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
        color: rgb(29, 47, 112);
      }
      .optionvideo :hover {
        cursor: pointer;
        text-decoration: underline;
      }
      .openimg {
        height: 40px;
        display: flex;
        font-weight: 550;
        font-size: 15px;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        color: rgb(29, 47, 112);
      }
      .checkTitle {
        width: 130px;
        height: auto;
        margin: 10px 0;
        white-space: normal;
        word-break: break-all;
        display: flex;
        justify-content: flex-start;
      }
    }
  }
  .bottom {
    width: 800px;
    margin: 0 auto;
    text-align: center;
    .bottomButton {
      margin: 0 auto;
      width: 100px;
    }
  }
  .isImg {
    position: absolute;
    right: 20px;
    width: 138px;
    height: 102px;
  }
  .isOverImg {
    position: absolute;
    left: 20px;
    width: 138px;
    height: 132px;
  }
  .elDog {
  }
}
</style>
